package first.niuke.simple;

import java.util.Arrays;

/**
 * @ClassName NC55
 * @description:
 * @author: 荣燊
 * @create: 2021-06-13 16:57
 **/
public class NC55 {
    public static void main(String[] args) {
        String[] str = {"abca","abc","abca","abc","abcc"};
        // abc
        System.out.println(longestCommonPrefix(str));
    }

    public static String longestCommonPrefix (String[] strs) {
        // write code here 最长公共前缀
        // 对字符串数组进行排序，比较第一和最后一个字符串就可以了
        if (strs == null || strs.length == 0) {
            return "";
        }
        Arrays.sort(strs);
        int len = strs.length;
        int len2 = Math.min(strs[0].length(), strs[len-1].length());
        int i = 0;
        for ( ; i<len2; i++) {
            if (strs[0].charAt(i) != strs[len-1].charAt(i)) {
                break;
            }
        }
        return strs[0].substring(0, i);
    }
}
